package com.miya.demo.common.enums;

import com.miya.demo.common.exception.BusinessException;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.EnumSet;
import java.util.Set;

/**
 * 模板消息类型枚举
 *
 * @author CaiXiaowei
 * @date 2022/08/31
 */
@Getter
@AllArgsConstructor
public enum TemplateMsgTypeEnum {

    // 消息类型 1-公众号 2-小程序
    PUBLIC(1, "公众号"),
    APPLETS(2, "小程序");

    private final Integer code;
    private final String desc;

    private static final Set<TemplateMsgTypeEnum> ALL = EnumSet.allOf(TemplateMsgTypeEnum.class);

    public static TemplateMsgTypeEnum getByCode(Integer code) {
        if (code == null) {
            return null;
        }

        return ALL.stream()
                .filter(o -> o.code.equals(code))
                .findAny().orElseThrow(() -> new BusinessException("模板消息类型: " + code + "未匹配到相关值！"));
    }

    public boolean is(Integer code) {
        return getCode().equals(code);
    }
}
